package org.feichao.imagegenerator;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * 图片素材
 * @author zhangzc@djcars.cn
 * @create 2016/8/9
 */
public class ImageBean extends Drawable{

    public BufferedImage image;

    public int x;

    public int y;

    public int rotateDegree = 0;

    public ImageBean(BufferedImage image, int x, int y) {
        this.image = image;
        this.x = x;
        this.y = y;
    }

    public ImageBean(File file, int x, int y) throws IOException {
        this(ImageIO.read(file), x, y);
    }

    public void rotate(int rotateDegree){
        this.rotateDegree = rotateDegree;
    }

    protected void doDraw(Context context) throws Exception {
        Graphics2D g=context.canvas.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        BufferedImage finalImg = image;
        if(rotateDegree % 360 != 0){
            finalImg = BufferedImageUtil.rotate(finalImg, rotateDegree);
        }
        g.drawImage(finalImg, x, y, finalImg.getWidth(), finalImg.getHeight(), null);
        g.dispose();
    }
}
